<template>
  <el-dialog
    :title="!agent.agentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="agent" :rules="dataRule" ref="agent" @keyup.enter.native="agentSubmit()" label-width="80px">
    <el-form-item label="经纪人名字" prop="agentName">
      <el-input v-model="agent.agentName" placeholder="经纪人名字"></el-input>
    </el-form-item>
    <el-form-item label="经纪人电话" prop="agentPhone">
      <el-input v-model="agent.agentPhone" placeholder="经纪人电话"></el-input>
    </el-form-item>
    <el-form-item label="公司id" prop="agentCompany">
      <el-input v-model="agent.agentCompany" placeholder="公司id"></el-input>
    </el-form-item>
    <el-form-item label="新房" prop="agentNewhouse">
      <el-input v-model="agent.agentNewhouse" placeholder="新房"></el-input>
    </el-form-item>
    <el-form-item label="二手房" prop="agentOldhouse">
      <el-input v-model="agent.agentOldhouse" placeholder="二手房"></el-input>
    </el-form-item>
    <el-form-item label="租房" prop="agentLeasehouse">
      <el-input v-model="agent.agentLeasehouse" placeholder="租房"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="agentStatus">
      <el-input v-model="agent.agentStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="经纪人头像" prop="agentImg">
      <el-input v-model="agent.agentImg" placeholder="经纪人头像"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="agentSort">
      <el-input v-model="agent.agentSort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="agentCreateTime">
      <el-input v-model="agent.agentCreateTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="agentUpdateTime">
      <el-input v-model="agent.agentUpdateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除时间" prop="agentDeleteTime">
      <el-input v-model="agent.agentDeleteTime" placeholder="逻辑删除时间"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="agentDelete">
      <el-input v-model="agent.agentDelete" placeholder="逻辑删除"></el-input>
    </el-form-item>
    <el-form-item label="服务介绍" prop="serviceIntroduction">
      <el-input v-model="agent.serviceIntroduction" placeholder="服务介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="agentSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        agent: {
          agentId: 0,
          agentName: '',
          agentPhone: '',
          agentCompany: '',
          agentNewhouse: '',
          agentOldhouse: '',
          agentLeasehouse: '',
          agentStatus: '',
          agentImg: '',
          agentSort: '',
          agentCreateTime: '',
          agentUpdateTime: '',
          agentDeleteTime: '',
          agentDelete: '',
          serviceIntroduction: ''
        },
        dataRule: {
          agentName: [
            { required: true, message: '经纪人名字不能为空', trigger: 'blur' }
          ],
          agentPhone: [
            { required: true, message: '经纪人电话不能为空', trigger: 'blur' }
          ],
          agentCompany: [
            { required: true, message: '公司id不能为空', trigger: 'blur' }
          ],
          agentNewhouse: [
            { required: true, message: '新房不能为空', trigger: 'blur' }
          ],
          agentOldhouse: [
            { required: true, message: '二手房不能为空', trigger: 'blur' }
          ],
          agentLeasehouse: [
            { required: true, message: '租房不能为空', trigger: 'blur' }
          ],
          agentStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          agentImg: [
            { required: true, message: '经纪人头像不能为空', trigger: 'blur' }
          ],
          agentSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          agentCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          agentUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          agentDeleteTime: [
            { required: true, message: '逻辑删除时间不能为空', trigger: 'blur' }
          ],
          agentDelete: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ],
          serviceIntroduction: [
            { required: true, message: '服务介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.agent.agentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['agent'].resetFields()
          if (this.agent.agentId) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/agentuser/info/${this.agent.agentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.agent.agentName = data.agentUser.agentName
                this.agent.agentPhone = data.agentUser.agentPhone
                this.agent.agentCompany = data.agentUser.agentCompany
                this.agent.agentNewhouse = data.agentUser.agentNewhouse
                this.agent.agentOldhouse = data.agentUser.agentOldhouse
                this.agent.agentLeasehouse = data.agentUser.agentLeasehouse
                this.agent.agentStatus = data.agentUser.agentStatus
                this.agent.agentImg = data.agentUser.agentImg
                this.agent.agentSort = data.agentUser.agentSort
                this.agent.agentCreateTime = data.agentUser.agentCreateTime
                this.agent.agentUpdateTime = data.agentUser.agentUpdateTime
                this.agent.agentDeleteTime = data.agentUser.agentDeleteTime
                this.agent.agentDelete = data.agentUser.agentDelete
                this.agent.serviceIntroduction = data.agentUser.serviceIntroduction
              }
            })
          }
        })
      },
      // 表单提交
      agentSubmit () {
        this.$refs['agent'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/agentuser/${!this.agent.agentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'agentId': this.agent.agentId || undefined,
                'agentName': this.agent.agentName,
                'agentPhone': this.agent.agentPhone,
                'agentCompany': this.agent.agentCompany,
                'agentNewhouse': this.agent.agentNewhouse,
                'agentOldhouse': this.agent.agentOldhouse,
                'agentLeasehouse': this.agent.agentLeasehouse,
                'agentStatus': this.agent.agentStatus,
                'agentImg': this.agent.agentImg,
                'agentSort': this.agent.agentSort,
                'agentCreateTime': this.agent.agentCreateTime,
                'agentUpdateTime': this.agent.agentUpdateTime,
                'agentDeleteTime': this.agent.agentDeleteTime,
                'agentDelete': this.agent.agentDelete,
                'serviceIntroduction': this.agent.serviceIntroduction
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
